package org.abratuhi.kvngroupware.gui;

import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.LoginController;
import org.abratuhi.kvngroupware.spring.SpringUtilities;

@SuppressWarnings("serial")
public class GUILogin extends JPanel{
	
	JLabel labelId = new JLabel();
	JLabel labelPassword = new JLabel();
	JTextField fieldId = new JTextField();
	JPasswordField fieldPassword = new JPasswordField();
	public JButton buttonRegistration = new JButton();
	public JButton buttonLogin = new JButton();
	
	KVNGroupwareClient kvnclient;
	LoginController controller;
	
	JFrame frame;
	
	public GUILogin(KVNGroupwareClient kvnclient, Properties locale){
		super();
		
		this.kvnclient = kvnclient;
		this.controller = new LoginController(kvnclient, this);
		
		this.buttonLogin.setText(locale.getProperty("login.button.login"));
		this.buttonRegistration.setText(locale.getProperty("login.button.register"));
		this.labelId.setText(locale.getProperty("login.label.id"));
		this.labelPassword.setText(locale.getProperty("login.label.password"));
		
		buttonRegistration.addActionListener(controller);
		buttonLogin.addActionListener(controller);
		
		//setPreferredSize(new Dimension(400,300));
		setVisible(true);
		setLayout(new SpringLayout());
		
		add(labelId);
		add(fieldId);
		add(labelPassword);
		add(fieldPassword);
		add(buttonRegistration);
		add(buttonLogin);
		
		SpringUtilities.makeCompactGrid(this, 3, 2, 0, 0, 0, 0);
	}
	
	public String getId(){
		return fieldId.getText();
	}
	public String getPassword(){
		String password = new String(fieldPassword.getPassword());
		fieldPassword.setText("");
		return password;
	}
	
	public void createJFrame(){
		frame = new JFrame("..:: KVNGroupware Client ::.."+"\t"+"Login");
		frame.getContentPane().add(this);
		frame.setVisible(true);
		frame.addWindowListener(this.controller);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}

}
